home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime vr / vrscript / common files / fileutilities.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  6.1 KB  |  242 lines

  1. //////////
  2. //
  3. //    File:        FileUtilities.c
  4. //
  5. //    Contains:    Some utilities for working with pathnames, files, and file specifications.
  6. //                All utilities start with the prefix "FileUtils_".
  7. //
  8. //    Written by:    Tim Monroe
  9. //
  10. //    Copyright:    © 1999 by Apple Computer, Inc., all rights reserved.
  11. //
  12. //    Change History (most recent first):
  13. //
  14. //       <1>         05/27/99    rtm        first file
  15. //
  16. //////////
  17.  
  18.  
  19. //////////
  20. //
  21. // header files
  22. //
  23. //////////
  24.  
  25. #ifndef __FileUtilities__
  26. #include "FileUtilities.h"
  27.  
  28.  
  29. //////////
  30. //
  31. // FileUtils_MakeFSSpecForPathName
  32. // Fill in the specified FSSpec, using the specified volume, directory, and pathname information.
  33. //
  34. // On Windows, the volume and directory info are ignored; if the pathname is not a full pathname,
  35. // create a full pathname using the current directory as a base path.
  36. //
  37. //////////
  38.  
  39. OSErr FileUtils_MakeFSSpecForPathName (short theVRefNum, long theDirID, char *thePathName, FSSpec *theFSSpec)
  40. {
  41. #if TARGET_OS_WIN32
  42. #pragma unused(theVRefNum, theDirID)
  43.     char        myFullPath[MAX_PATH];
  44. #endif
  45.     OSErr        myErr = noErr;
  46.     
  47.     if ((thePathName == NULL) || (theFSSpec == NULL))
  48.         return(paramErr);
  49.     
  50. #if TARGET_OS_MAC
  51.     // on the MacOS, it doesn't matter whether thePathName is a full pathname or not,
  52.     // since FSMakeFSSpec will do the right thing in either case
  53.     myErr = FSMakeFSSpec(theVRefNum, theDirID, c2pstr(thePathName), theFSSpec);
  54. #endif
  55. #if TARGET_OS_WIN32
  56.     if (FileUtils_IsFullPathName(thePathName)) {
  57.         myErr = FSMakeFSSpec(0, 0L, c2pstr(thePathName), theFSSpec);
  58.     } else {
  59.         // thePathName is not a full pathname, so construct a full pathname relative
  60.         // to the current directory; if doing so would make a pathname that's too large,
  61.         // then just return an error
  62.         GetCurrentDirectory(MAX_PATH, myFullPath);
  63.         
  64.         if (strlen(myFullPath) + strlen(kFilePathSepString) + strlen(thePathName) + 1 > MAX_PATH)
  65.             return(paramErr);
  66.             
  67.         strcat(myFullPath, kFilePathSepString);
  68.         strcat(myFullPath, thePathName);
  69.         
  70.         myErr = FSMakeFSSpec(0, 0L, c2pstr(myFullPath), theFSSpec);
  71.     }
  72. #endif
  73.     
  74.     return(myErr);
  75. }
  76.  
  77.  
  78. //////////
  79. //
  80. // FileUtils_MakeFSSpecForAnyFileInDir
  81. // Return, through theFileFSSpec, a file specification for a file (possibly nonexistent) in the specified directory.
  82. //
  83. //////////
  84.  
  85. OSErr FileUtils_MakeFSSpecForAnyFileInDir (Str255 thePathName, FSSpecPtr theFileFSSpec)
  86. {
  87.     Str255        myString;
  88.     short        myLength = thePathName[0];
  89.     OSErr        myErr = noErr;
  90.     
  91.     // make sure we have enough space to add the path separator and a 1-character
  92.     // file name onto thePathName
  93.     if (myLength + 2 > 255)
  94.         return(paramErr);
  95.  
  96.     BlockMove(&thePathName[1], &myString[1], myLength);
  97.     myString[myLength + 1] = kFilePathSeparator;
  98.     myString[myLength + 2] = 'a';    // a short and simple bogus file name
  99.     myString[0] = myLength + 2;
  100.     myErr = FSMakeFSSpec(0, 0L, myString, theFileFSSpec);
  101.     
  102.     return(myErr);
  103. }
  104.  
  105.  
  106. //////////
  107. //
  108. // FileUtils_IsFullPathName
  109. // Is the specified pathname a full pathname?
  110. //
  111. // NOTE: THIS IS QUICK AND DIRTY, AND NEEDS TO BE REWORKED
  112. //
  113. //////////
  114.  
  115. static Boolean FileUtils_IsFullPathName (char *thePathName)
  116. {
  117. #if TARGET_OS_MAC
  118.     // we need to do some work here, eh?
  119.     return(true);
  120. #endif
  121.  
  122. #if TARGET_OS_WIN32
  123.     // on Windows, a full pathname always begins with a disk designator or a server name;
  124.     // for the time being, we will support *local* files only
  125.     if ((strlen(thePathName) >= 2) && (URLUtils_IsAlphabetic(thePathName[0])) && (thePathName[1] == kWinVolumeNameChar))
  126.         return(true);
  127.     else
  128.         return(false);
  129. #endif
  130. }
  131.  
  132.  
  133. //////////
  134. //
  135. // FileUtils_GetBaseName
  136. // Return the basename of the specified pathname.
  137. //
  138. // Based heavily on URLUtils_GetURLBasename.
  139. //
  140. //////////
  141.  
  142. char *FileUtils_GetBaseName (char *thePathName)
  143. {
  144.     char    *myBasename = NULL;
  145.     short    myLength = 0;
  146.     short    myIndex;
  147.  
  148.     // make sure we got a pathname passed in
  149.     if (thePathName == NULL)
  150.         goto bail;
  151.         
  152.     // get the length of the pathname
  153.     myLength = strlen(thePathName);
  154.     
  155.     // find the position of the rightmost file path separator in thePathName
  156.     if (strchr(thePathName, kFilePathSeparator) != NULL) {
  157.  
  158.         myIndex = myLength - 1;
  159.         while (thePathName[myIndex] != kFilePathSeparator)
  160.             myIndex--;
  161.             
  162.         // calculate the length of the basename
  163.         myLength = myLength - myIndex - 1;
  164.  
  165.     } else {
  166.         // there is no rightmost file path separator in thePathName;
  167.         // set myIndex so that myIndex + 1 == 0, for the call to BlockMove below
  168.         myIndex = -1;
  169.     }
  170.     
  171.     // allocate space to hold the string that we return to the caller
  172.     myBasename = malloc(myLength + 1);
  173.     if (myBasename == NULL)
  174.         goto bail;
  175.         
  176.     // copy into myBasename the substring of thePathName from myIndex + 1 to the end
  177.     BlockMove(&thePathName[myIndex + 1], myBasename, myLength);
  178.     myBasename[myLength] = '\0';
  179.     
  180. bail:    
  181.     return(myBasename);
  182. }
  183.  
  184.  
  185. //////////
  186. //
  187. // FileUtils_ChangeFileNameSuffix
  188. // Change the suffix of the specified filename to the specified suffix;
  189. // return the new filename as the function result.
  190. //
  191. //////////
  192.  
  193. char *FileUtils_ChangeFileNameSuffix (char *thePathName, char *theNewSuffix)
  194. {
  195.     char        *myNewName = NULL;
  196.     short        myLength = 0;
  197.     short        myIndex;
  198.     Boolean        myNeedSep;
  199.  
  200.     // make sure we got a pathname passed in
  201.     if (thePathName == NULL)
  202.         goto bail;
  203.         
  204.     // get the length of the pathname
  205.     myLength = strlen(thePathName);
  206.     
  207.     // find the position of the rightmost file suffix separator in thePathName
  208.     if (strchr(thePathName, kFileSuffixSeparator) != NULL) {
  209.         myNeedSep = false;
  210.         
  211.         myIndex = myLength - 1;
  212.         while (thePathName[myIndex] != kFileSuffixSeparator)
  213.             myIndex--;
  214.             
  215.         // calculate the length of the filename less the suffix, but including the separator
  216.         myLength = myIndex + 1;
  217.  
  218.     } else {
  219.         // there is no rightmost file suffix separator in thePathName
  220.         myNeedSep = true;
  221.     }
  222.     
  223.     // allocate space to hold the string that we return to the caller
  224.     myNewName = malloc(myLength + (myNeedSep ? 1 : 0) + strlen(theNewSuffix));
  225.     if (myNewName == NULL)
  226.         goto bail;
  227.         
  228.     // copy into myBasename the substring of thePathName from the start to position myIndex
  229.     BlockMove(thePathName, myNewName, myLength);
  230.     if (myNeedSep)
  231.         strcat(myNewName, kFileSuffixSepString);
  232.     strcat(myNewName, theNewSuffix);
  233.     
  234.     myNewName[myLength + (myNeedSep ? 1 : 0) + strlen(theNewSuffix)] = '\0';
  235.     
  236. bail:    
  237.     return(myNewName);
  238. }
  239.  
  240.  
  241.  
  242. #endif    // ifndef __FileUtilities__